package com.mall.mymall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.mymall.dao.dto.CommentsDto;
import com.mall.mymall.dao.dto.QueryDto;
import com.mall.mymall.dao.dto.querypage.AddressPageRequest;
import com.mall.mymall.dao.entity.Addresses;

import com.mall.mymall.mapper.AddressesMapper;
import com.mall.mymall.service.AddressesService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressesServiceImpl extends ServiceImpl<AddressesMapper, Addresses> implements AddressesService {

    @Override
    public Addresses getAddressesByNameAndUserId(String address, Integer userId) {
        return baseMapper.getAddressesByNameAndUserId(address, userId);
    }

    @Override
    public List<Addresses> findByUserId(Integer userId) {
        return baseMapper.findByUserId(userId);
    }

    /**
     * 根据用户ID和分页请求参数，分页查询地址信息
     *
     * @param addressPageRequest 包含用户信息和分页请求参数的对象
     * @return 返回一个QueryDto对象，其中包含地址信息的列表和总记录数
     */
    @Override
    public QueryDto<Addresses> pageAddresses(AddressPageRequest addressPageRequest) {
        // 创建分页对象
        Page<Addresses> page = new Page<>(addressPageRequest.getPageRequestDto().getPage(), addressPageRequest.getPageRequestDto().getSize());
        // 获取用户ID
        Integer userId = addressPageRequest.getUser().getUserId();
        // 执行自定义分页查询
        IPage<Addresses> resultPage = this.baseMapper.pageAddresses(page, userId);
        // 构造返回结果
        QueryDto<Addresses> result = new QueryDto<>();
        result.setTotal((int) resultPage.getTotal());
        result.setRecords(resultPage.getRecords());
        return result;
    }
}
